﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SWEN_HMS.Classes;

namespace SWEN_HMS.StaffHousekeepingModule
{
    public partial class UpdateSchedule : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    IfRemarksIsYesOrNo();
                    if (Request.QueryString["hkID"] != null)
                    {
                        int housekeepingIDFromQS = Convert.ToInt32(Request.QueryString["hkID"]);
                        Housekeeping result = StaffHousekeepingDBManager.getScheduleForUpdate(housekeepingIDFromQS);
                        if (result != null)
                        {
                            housekeepingIDLabel.Text = Convert.ToString(housekeepingIDFromQS);
                            List<int> resultSetForStaffID = StaffHousekeepingDBManager.getStaffIDHousekeepingList();
                            List<int> resultSetForRoomNo = StaffHousekeepingDBManager.getRoomNumList();
                            if (resultSetForStaffID != null)
                            {
                                staffIDListBox.DataSource = resultSetForStaffID;
                                staffIDListBox.DataBind();
                                staffIDListBox.SelectedIndex = staffIDListBox.Items.IndexOf(staffIDListBox.Items.FindByText(Convert.ToString(result.staffID)));
                                stfNameLabel.Text = StaffHousekeepingDBManager.getStaffNameByStaffIDHousekeeping(Convert.ToInt32(staffIDListBox.SelectedValue));
                                stfPositionLabel.Text = StaffHousekeepingDBManager.getStaffPositionHousekeeping(Convert.ToInt32(staffIDListBox.SelectedValue));
                                if (stfPositionLabel.Text != "Room Maintenance")
                                {
                                    RequiredFieldValidator2.Enabled = false;
                                }

                            }
                            if (resultSetForRoomNo != null)
                            {
                                roomNumListBox.DataSource = resultSetForRoomNo;
                                roomNumListBox.DataBind();
                                if (result.roomNo != 0)
                                {
                                    roomNumListBox.SelectedIndex = roomNumListBox.Items.IndexOf(roomNumListBox.Items.FindByText(Convert.ToString(result.roomNo)));
                                }
                            }
                            remarksTextBox.Text = result.remarks;
                            jobDateTextbox.Text = result.dutyDateTime.ToString("dd/MM/yyyy");
                            jobTimeHoursDropDown.SelectedIndex = jobTimeHoursDropDown.Items.IndexOf(jobTimeHoursDropDown.Items.FindByText(Convert.ToString(result.dutyDateTime.Hour)));
                            jobTimeMinsTextbox.Text = Convert.ToString(result.dutyDateTime.Minute);

                        }
                        else
                        {
                            Response.Redirect("~/StaffHousekeepingModule/ViewSchedule.aspx?updateSuccessful=no");
                        }
                    }
                    else
                    {
                        Response.Redirect("~/StaffHousekeepingModule/ViewSchedule.aspx?updateSuccessful=no");
                    }
                }
                Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
                Response.Cache.SetValidUntilExpires(false);
                Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Cache.SetNoStore();
            }
            Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            Response.Cache.SetValidUntilExpires(false);
            Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
        }

        protected void staffIDListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            stfNameLabel.Text = StaffHousekeepingDBManager.getStaffNameByStaffIDHousekeeping(Convert.ToInt32(staffIDListBox.SelectedValue));
        }

        protected void scheduleRadioButton_SelectedIndexChanged(object sender, EventArgs e)
        {
            IfRemarksIsYesOrNo();
        }

        protected void IfRemarksIsYesOrNo()
        {
            if (remarksRadioButton.SelectedValue == "Yes")
            {
                if (remarksTextBox.Enabled == false)
                {
                    remarksTextBox.Enabled = true;
                }
            }
            else
            {
                if (remarksTextBox.Enabled == true)
                {
                    remarksTextBox.Enabled = false;
                }
            }
        }

        protected void submitSchedBtn_Click(object sender, EventArgs e) // update button
        {
            Housekeeping hk = new Housekeeping();
            hk.housekeepingID = Convert.ToInt32(housekeepingIDLabel.Text);
            hk.staffID = Convert.ToInt32(staffIDListBox.SelectedValue);
            if (stfPositionLabel.Text == "Room Maintenance")
            {
                hk.roomNo = Convert.ToInt32(roomNumListBox.SelectedValue);
            }
            hk.remarks = remarksTextBox.Text;
            hk.dutyDateTime = DateTime.Parse(jobDateTextbox.Text + " " + jobTimeHoursDropDown.SelectedValue + ":" + jobTimeMinsTextbox.Text);
            bool ifDeleted = StaffHousekeepingDBManager.updateSchedule(hk, stfPositionLabel.Text);
            if (ifDeleted == true)
            {
                outcomeLabel.Text = "Schedule/Housekeeping ID: <b><u>" + housekeepingIDLabel.Text + "</u></b> has been successfully updated!";
            }
            else
            {
                outcomeLabel.Text = "Housekeeping Schedule not successfully updated!";
            }
        }

        protected void backToViewScheduleBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/StaffHousekeepingModule/ViewSchedule.aspx");
        }
    }
}